home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Dev / ACE_Prgs.lha / file / fileread.b < prev   
Text File  |  1994-11-24  |  1KB  |  49 lines

  1. {* 
  2. ** This program reads a file into a dynamically allocated string 
  3. ** buffer.
  4. **
  5. ** The section delimited by "***" is the guts of the program. The
  6. ** rest is just support code.
  7. **
  8. ** See also the ACE docs re: INPUT$ for a similar but more limited
  9. ** method.
  10. *}
  11.  
  12. CONST NULL = 0&
  13.  
  14. ADDRESS fh, buf
  15. LONGINT length, count
  16.  
  17. DECLARE FUNCTION LONGINT xRead(ADDRESS fileHandle, ADDRESS buffer, ~
  18.                        LONGINT length) LIBRARY
  19.  
  20. SUB quit(SHORTINT errNum)
  21.   CASE
  22.     errNum = 0 : PRINT "Unable to open file."
  23.     errNum = 1 : PRINT "Unable to allocate memory." : CLOSE #1
  24.     errNum = 2 : PRINT "Read error." : CLOSE #1
  25.   END CASE
  26.  
  27.   STOP        '..Quit.
  28. END SUB
  29.  
  30. theFile$ = FILEBOX$("Select a text file:")
  31.  
  32. '***
  33. OPEN "I",#1,theFile$            '..Open file for input.
  34. fh = HANDLE(1)                '..Get AmigaDOS file handle.
  35. IF fh = NULL THEN CALL quit(0)
  36. length = LOF(1)                '..Get length of file in bytes.
  37. buf = ALLOC(length)+1            '..Allocate an ASCII-0 filled buffer.
  38. IF buf = NULL THEN CALL quit(1)
  39. count = xRead(fh, buf, length)        '..Read file contents into buffer.
  40. IF count <> length THEN CALL quit(2)
  41. CLOSE #1
  42. '***
  43.  
  44. STRING myFileBytes ADDRESS buf        '..Associate buffer with a string.
  45.  
  46. PRINT myFileBytes            '..Do something with the string.
  47. PRINT
  48. PRINT LEFT$(myFileBytes,80)
  49.